package com.yanghui.lingyue.functions.ktBase

import com.icedata.sika.bridge.SikaEvent
import com.icedata.sika.bridge.SikaGroupEvent
import com.icedata.sika.bridge.SikaQuoteMessage
import com.icedata.sika.bridge.SikaUserEvent
import com.icedata.sika.core.CommandProcessor
import com.icedata.sika.core.Sika
import com.icedata.sika.database.Column
import com.icedata.sika.database.Table
import com.yanghui.lingyue.bot.Bot
import com.yanghui.lingyue.data.Command
import com.yanghui.lingyue.data.Operation
import com.yanghui.lingyue.functions.AbstractFunction
import com.yanghui.lingyue.utils.functionUtils.FunctionUtils
import java.util.*

object PornHigh : AbstractFunction() {
    override val name: String = "PornHigh"

    override suspend fun parse(event: SikaEvent, sika: Sika): LinkedList<Command> {
        val command = LinkedList<Command>()
        if (event is SikaUserEvent) return command
        event as SikaGroupEvent
        if (!event.message.hasQuoteMessage) return command
        if (!FunctionUtils.atBot(event)) return command
        if (!FunctionUtils.checkValid(event)) return command
        if (event.sendGroup.id !in enableGroup) return command
        val param = FunctionUtils.extractKeyword(event, "迫害")
        param?.let {
            if (it.contains("获取"))
                command.add(Command(event, "$name -Get", it))
            else
                command.add(Command(event, "$name -Insert", it))
        }
        return command
    }

    override suspend fun execute(command: Command, sika: Sika): CommandProcessor.Result {
        val instructionList = command.instruction.split(" ")
        val operation = Operation(command)
        when(instructionList[1]) {
            "-Get" -> {
                val sql = "SELECT * FROM PornHigh WHERE groupID = ?"
                val record = PornHighRecord()
                Bot.sikaCore.getStatement(sql).let {
                    it.setLong(1, command.sendGroup!!.id)
                    it.executeQuery().let { result->
                        result.next()
                        record.message = result.getNString("message")
                        record.senderId = result.getLong("senderID")
                    }
                }

            }
            "-Insert" -> {
                for (m in command.event.message) {
                    if (m is SikaQuoteMessage) {
                        PornHighRecord().let {record->
                            record.groupID = command.sendGroup!!.id
                            record.senderId = command.sender.id
                            record.message = m.contentToString()
                            record.operationID = operation.operationID
                            Bot.sikaCore.databaseInsert(record)
                        }
                    }
                }
            }
        }
        return CommandProcessor.Result.ADOPT
    }
}

@Table("PornHigh", "", "operationID")
data class PornHighRecord(

    @Column("operationID")
    var operationID: Long,

    @Column("senderID")
    var senderId: Long,

    @Column("message")
    var message: String,

    @Column("groupID")
    var groupID: Long
) {
    constructor(): this(0,0,"",0)
}